/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.map.scalar;

import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import technotheist.geo.map.AIntMap;
import technotheist.geo.map.Format;

/**
 *
 * @author Mitchel Strachan
 */
public class UShortMap extends AIntMap<ShortBuffer> {

    static final int USHORT_MAX = Short.MAX_VALUE - Short.MIN_VALUE;
    final ShortBuffer data;

    public UShortMap(int sideLength, boolean normalized) {
        this(sideLength, 1, normalized);
    }

    public UShortMap(int sideLength, int components, boolean normalized) {
        super(sideLength, components, Format.UShort, normalized);
        this.data = ByteBuffer.allocateDirect(byteCount).asShortBuffer();
    }

    @Override
    protected ShortBuffer data() {
        return data;
    }

    @Override
    public int getInt(int bufferIndex) {
        return data.get(bufferIndex) - Short.MIN_VALUE;
    }

    @Override
    public void setInt(int bufferIndex, int value) {
        if (value < 0 || value > USHORT_MAX) {
            throw new IllegalArgumentException("value must be between " + 0 + " and " + USHORT_MAX);
        }
        data.put(bufferIndex, (short) (value + Short.MIN_VALUE));
    }
}
